<template>
    <div>
    <basic-container>
      <avue-crud
        :option="tableOption"
        :data="tableData"
        :table-loading="tableLoading"
        :page="page"
        ref="crud"        
        
        
        @refresh-change="handleRefreshChange"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        @selection-change="handleSelectionChange"
        
        @search-change="handleSearchChange"
      >
      <template slot="menu" scope="scope">
        <div>
           <el-button
        type="text"
        @click="handleResole(scope.row)"
        v-perm="'/admin/user_withdraw/check'"
        v-if="scope.row.status==1"
        size="small"
      >审核通过</el-button>
           <el-button
        type="text"
        v-if="scope.row.status==1"
        @click="handleReject(scope.row)"
        v-perm="'/admin/user_withdraw/check'"
        size="small"
      >审核拒绝</el-button>
           <el-button
        type="text"
        @click="handleProcess(scope.row)"
        v-if="scope.row.status==2"
        v-perm="'/admin/user_withdraw/process'"
        size="small"
      >确认打款</el-button>
      
        </div>
      </template>
      <template slot="menuLeft">
        
        <el-button
        type="primary"
        @click="handleExport"
        :loading="downloadLoading"
        icon="el-icon-download"
        v-perm="'/admin/user_withdraw/export'"
        size="small"
      >导出</el-button>
      </template>
      </avue-crud>
      </basic-container>
      <el-dialog :visible.sync="processDialogShowed" title="打款确认" width="900px">
          <!-- <el-form :model="currentRow" label-width="100px" v-if="currentRow">
              <el-form-item label="提现金额">
                {{currentRow.withdrawAmount}}
              </el-form-item>
              <el-form-item label="银行卡持卡人">
                {{currentRow.bankAccountUserName}}
              </el-form-item>
              <el-form-item label="银行">
                {{currentRow.bankName}}
              </el-form-item>
              <el-form-item label="银行卡卡号">
                {{currentRow.bankAccountNo}}
              </el-form-item>
          </el-form> -->
          <avue-form ref="form1" @submit="handleSubmit" v-model="currentRow" :option="userWithdrawTableOption2"></avue-form>
          <div slot="footer" class="dialog-footer">
             <el-button
              type="info"
              @click="processDialogShowed=false"
              :loading="isSubmiting"
            >取消</el-button>
             <el-button
              type="primary"
              @click="handleDoProcess"
              :loading="isSubmiting"
            >确认打款</el-button>
          </div>
      </el-dialog>
    </div>
  </template>
  <script>
  import { validatenull } from "@/util/validate";
  import { filterForm } from "@/util/util";
  // import { DIC } from "@/const/dic";
  import {  getLabelFromDicV2 } from "@/const/dic";
  import { nowDate } from "../../util/date";
  import * as apiObj from "../../api/userWithdraw";
  import { userWithdrawTableOption,userWithdrawTableOption2} from "@/const/userWithdraw/userWithdrawTableOption.js";
  
  export default {
    data() {
      return {
        bachDeleting: false,
        tableOption: {}, //表格设置属性
        tableData: [], //表格的数据
        downloadLoading:false,
        isSubmiting:false,
        userWithdrawTableOption2,
        processDialogShowed:false,
        currentRow:null,
        tableLoading: false,
        tableQueryParam: {},
        page: {
          total: 0, //总页数
          currentPage: 1, //当前页数
          pageSize: 10 //每页显示多少条
        }
      };
    },
    created() {
      //初始化数据格式
      this.tableOption = userWithdrawTableOption;
      this.handleList();
    },
    methods: {
      handleResole(row){
        this.$prompt('确认审核通过吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPlaceholder:'审核备注'
        }).then(({ value }) => {
            value = value || '';
            apiObj.check({
              id:row.id,
              status:2,
              checkRemark:value.trim()
          }).then(() => {
            this.success();
            this.handleList();
          });
        }).catch(() => {
          
        });
      },
      handleReject(row){
          this.$prompt('确认审核拒绝吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPlaceholder:'审核备注'
        }).then(({ value }) => {
            value = value || '';
            apiObj.check({
              id:row.id,
              status:3,
              checkRemark:value.trim()
          }).then(() => {
            this.success();
            this.handleList();
          });
        }).catch(() => {
          
        });
      },
      handleProcess(row){
        /** 
        this.$prompt('确认打款了吗', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          inputPlaceholder:'审核备注'
        }).then(({ value }) => {
            value = value || '';
            apiObj.process({
              id:row.id,
              processAccessory:'',
              processRemark:value.trim()
          }).then(() => {
            this.success();
            this.handleList();
          });
        }).catch(() => {
          
        });
         **/
         this.currentRow = row;
         this.currentRow.processRemark = '';
         this.processDialogShowed = true;
      },
      handleDoProcess(){
        this.$refs.form1.validate(v=>{
          if(!v){
            return;
          }
            this.isSubmiting = true;
          apiObj.process({
                id:this.currentRow.id,
                processAccessory:this.currentRow.processAccessory,
                processRemark:this.currentRow.processRemark.trim()
            }).then(() => {
              this.processDialogShowed = false;
              this.success();
              this.handleList();
            }).finally(()=>{
              this.isSubmiting = false;
            });
        });
        
      },
      handleCurrentChange(page) {
        this.page.currentPage = page;
        this.handleList();
      },
      handleSizeChange(size) {
        this.page.pageSize = size;
        this.handleList();
      },
      handleSubmit(){

      },
      handleSelectionChange(args) {
        this.currentSelectedRowIds = args.map(el => el.id);
      },
      hasChanged(params1,params2){
        return Object.keys(params1).some(key=>params2[key] != params1[key]);
    },
    handleSearchChange(params) {
      if(this.hasChanged(params,this.tableQueryParam)){
        this.page.currentPage = 1;
      }
        this.tableQueryParam = params;
        this.handleList();
      },
            
      
      handleRefreshChange() {
        this.handleList();
      },
      /**
       * @title 获取数据
       * @detail 赋值为tableData表格即可
       *
       **/
      handleList() {
        this.tableLoading = true;
        apiObj.list({
          pageNo: this.page.currentPage,
          pageSize: this.page.pageSize,
          ...this.tableQueryParam
        })
          .then(data => {
            this.tableData = data.data || data.dataList || [];
            this.page.total = data.total;
            this.tableLoading = false;
          })
          .catch(() => {
            this.tableLoading = false;
          });
      },     
          
      handleExport() {
        this.downloadLoading = true;
        apiObj.list({
          pageNo: 1,
          pageSize: 3000,
          ...filterForm(this.$refs.crud.searchForm)
        }).then(res => {
          import("@/vendor/Export2Excel").then(excel => {
            const columns = userWithdrawTableOption.column.filter(el=>el.hide !== true && el.type!='upload');
            const tHeader = columns.map(el=>el.label);
            const filterVal = columns.map(el=>el.prop);
            const data = this.formatJson(filterVal, res.data || res.dataList);
            excel.export_json_to_excel({
              header: tHeader,
              data,
              filename: "用户提现信息" + nowDate()
            });
            this.downloadLoading = false;
          });
        });
      },
      formatJson(filterVal, jsonData) {      
        return jsonData.map(v =>
          filterVal.map(j => {
            return getLabelFromDicV2(this.$refs.crud.DIC,j,v[j]);
          })
        );
      }
    }
  };
  </script>
